package com.wtb.javatool.service.impl;

import com.wtb.javatool.common.PageBean;
import com.wtb.javatool.dao.UserMapper;
import com.wtb.javatool.dto.UserDto;
import com.wtb.javatool.entity.Student;
import com.wtb.javatool.entity.User;
import com.wtb.javatool.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    private final UserMapper userMapper;

    @Autowired
    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    /**
     * 根据用户ID、用户名或手机号查询普通项目用户。
     *
     * @param userId   用户ID
     * @param userName 用户名
     * @param phone    手机号
     * @return 查找到的用户列表，如果未找到则返回空列表。
     */
    @Override
    public List<User> findUser(Long userId, String userName, String phone, Integer projectType, Integer userType) {
        if (projectType == 1)
            return userMapper.findNormalUser(userId, userName, phone);
        else {
            if (userType == 1) return userMapper.findManagerUser(userId, userName, phone);
            else if (userType == 2) return null;
            else return userMapper.findStudentUser(userId,userName,phone);

        }


    }

    @Override
    public PageBean<UserDto> findClassStudents(Long classId, Long teamId, Long studentId, String userName, Integer haveTeam, Integer currentPage, Integer size) {
        PageBean<UserDto> userDtoPageBean = new PageBean<>();
        userDtoPageBean.setRows(userMapper.findClassStudents(classId,teamId,studentId,userName,haveTeam,(currentPage-1)*size,size));
        userDtoPageBean.setTotalCount(userMapper.findClassStudentsCount(classId,teamId,studentId,userName,haveTeam));
    return userDtoPageBean;
    }

    @Override
    public void setTeamForStudents(Long classId, Long teamId, List<Long> studentIds) {
        userMapper.setTeamForStudents(classId, teamId, studentIds);
    }

    @Override
    public PageBean<Student> findAllStudents(Long classId,
                                             Long studentId,
                                             String studentName,
                                             Integer currentPage,
                                             Integer size) {
        PageBean<Student> studentPageBean = new PageBean<>();
        studentPageBean.setRows(userMapper.findAllStudents(studentId,studentName,classId,(currentPage-1)*size,size));
        studentPageBean.setTotalCount(userMapper.findAllStudentsCount(studentId,studentName,classId));
        return studentPageBean;
    }

    @Override
    public String addUser(Long userId,
                          Long bandId,
                          String password,
                          String email,
                          String phone,
                          String role,
                          String studentName,
                          String teacherName){
        //获取学生姓名和老师姓名
        // 3. 写入用户表
        String username = "student".equals(role) ? studentName : teacherName;
        User user = new User();
        user.setUserId(userId);
        user.setUserName(username);
        user.setPhone(phone);
        user.setEmail(email);
        user.setPassword(password);
        user.setCreatedAt(Date.from(Instant.now()));
        user.setUpdatedAt(Date.from(Instant.now()));
        // 4. 写入用户表
        int userResult = userMapper.insertUser(user);
        if (userResult <= 0) {
            throw new RuntimeException("用户注册失败");
        }
        if ("student".equals(role)) {
            //新增内容至studenet
            int studentResult = userMapper.insertStudent(userId,bandId,studentName,phone);
            int roleResult = userMapper.insertUserRole(userId, 0);
            if (studentResult <= 0) {
                throw new RuntimeException("学生信息保存失败");
            }
            if (roleResult <= 0) {
                throw new RuntimeException("角色信息保存失败");
            }
        } else {
            //新增内容至teacher
            int teacherResult = userMapper.insertTeacher(userId,bandId,teacherName);
            int roleResult = userMapper.insertUserRole(userId, 1);
            if (teacherResult <= 0 ) {
                throw new RuntimeException("教师信息保存失败");
            }
            if (roleResult <= 0) {
                throw new RuntimeException("角色信息保存失败");
            }
        }
        return "注册成功";
    }

    public  User findUserByPassword(Long userId,String password, String role){
        User user;
        if ("student".equals(role))
            user = userMapper.findUserByPassword(userId,password,0);
        else
            user = userMapper.findUserByPassword(userId,password,1);
        if (user!=null)
            return user;
        else
            return null;
    }
}
